<?php namespace App\Http\Controllers;

use App\Process;
use Auth;
use App\Pub;
use App\Commodity;
use App\Grouptemplate;
use App\House;
use Redirect;
use Input;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;

class CommoditiesController extends Controller {

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public $ACT;
    public $USER;
    protected $auth;
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
        $this->USER = Auth::user();
        $this->ACT = 'commodities';
//		$this->middleware('auth');
    }

    public function index(){
        $ACT = $this->ACT;
        $METHOD = 'flows';
        $pub = Pub::where('id','>',1)->get();
        $commodities = Commodity::all();
        $coms = array();
        foreach($commodities as $commodity){
            foreach($pub as $type){
                if($commodity->type == $type->id){
                    $coms[$type->id][] = $commodity;
                }
            }
        }
        return view('commodities.index',compact('ACT','METHOD','pub','coms'));
    }

    public function store(){
        $commodity = new Commodity();
        $commodity->info = serialize(Input::all());
        $commodity->name = Input::get('name');
        $commodity->price = Input::get('price');
        $commodity->user_id = $this->USER->id;
        $commodity->type = Input::get('type_id');
        $commodity->save();
    }

    public function flows(){
        $templates = Grouptemplate::whereHas('template', function($q)
        {
            $q->where('type','commodity');

        })->where('group_id',$this->USER->group_id)->get();


        $group_id = $this->USER->group_id;
        $unit_id = $this->USER->unit_id;
        $user_id = $this->USER->id;
        $processes = Process::Where(function($query) use ($group_id,$unit_id)
                        {
                            $query->where('use_type','group')
                                ->where('use_id',$group_id)
                                ->where('status',1)
                                ->where('next_unit_id',$unit_id)
                                ->where('next_unit_type','!=',0);
                        })
                        ->orWhere(function($query) use ($group_id,$user_id)
                        {
                            $query->where('use_type','group')
                                ->where('use_id',$group_id)
                                ->where('status',1)
                                ->where('next_unit_type',0);
                        })
                        ->orWhere(function($query) use ($user_id)
                        {
                            $query->where('use_type','user')
                                ->where('status',1)
                                ->where('use_id',$user_id);
                        })
                        ->paginate(30);



        $ACT = $this->ACT;
        $METHOD = 'flows';
        return view('commodities.flows',compact('ACT','METHOD','templates','processes'));
    }

    public function done(){
        $templates = Grouptemplate::whereHas('template', function($q)
        {
            $q->where('type','commodity');

        })->where('group_id',$this->USER->group_id)->get();


        $group_id = $this->USER->group_id;
        $unit_id = $this->USER->unit_id;
        $user_id = $this->USER->id;
        $processes = Process::whereHas('userprocess', function($q) use($user_id)
        {
            $q->where('user_id',  $user_id);

        })->paginate(30);



        $ACT = $this->ACT;
        $METHOD = 'flows';
        return view('commodities.flows',compact('ACT','METHOD','templates','processes'));
    }

    public function houses(){
        $ACT = $this->ACT;
        $METHOD = 'houses';

        $commodity_type = 2;

        $houses = House::whereHas('commodity', function($q) use($commodity_type)
        {
            $q->where('type',  $commodity_type);

        })
            ->where('unit_id',$this->USER->unit_id)
            ->paginate(30);

        return view('commodities.houses',compact('ACT','METHOD','houses'));
    }
}
